﻿var PhoneCommunicator = function() {
    var isAndroid = /android/i.test(navigator.userAgent.toLowerCase());
    var isiPad = /ipad/i.test(navigator.userAgent.toLowerCase());
    var isiPhone = /iphone/i.test(navigator.userAgent.toLowerCase());
    var isiPod = /ipod/i.test(navigator.userAgent.toLowerCase());
    var isiOS = isiPad || isiPhone || isiPod;

    var menuVisible = false;

    var AndroidInterface = function() {
        return {
            getPicture: function(titleText, cameraText, galleryText, cancelText) {
                Android.getPicture(titleText, cameraText, galleryText, cancelText);
            },
            getDeviceId: function() {
                return Android.getDeviceId();
            },
            hideSplashscreen: function() {
                Android.hideSplashscreen();
            },
            showSplashscreen: function() {
                Android.showSplashscreen();
            },
            setPageTitle: function(title) {
                Android.setPageTitle(title);
            },
            showMenu: function() {
                if (!menuVisible) {
                    menuVisible = true;
                    Android.showMenu();
                }
            },
            hideMenu: function() {
                Android.hideMenu();
            },
            exitApplication: function() {
                Android.exitApplication();
            },
            setUserSettings: function(data) {
                Android.setUserSettings(data);
            },
            getUserSettings: function() {
                var data = Android.getUserSettings();
                if (!data)
                    return null;
                var settings = JSON.parse(data);
                return settings;
            },
            getAuthData: function() {
                var data = Android.getAuthData();
                if (!data)
                    return null;
                var auth = JSON.parse(data);
                return auth;
            },
            setAuthData: function(data) {
                Android.setAuthData(data);
            },
            deleteAuthData: function() {
                Android.deleteAuthData();
            },
            setModuleAction: function(action) {
                Android.setModuleAction(action);
            },
            setNotifications: function(nr) {
                Android.setNotifications(nr);
            },
            updateApplication: function(appStoreUrl) {
                Android.updateApplication(appStoreUrl);
            },
            disconnect: function() {
                Android.disconnect();
            },
            setServer: function(serverCode) {
                Android.setServer(serverCode);
            },
            loadApplication: function(serverCode, username, password) {
                Android.loadApplication(serverCode, username, password);
            },
            getConnectionData: function() {
                Android.getConnectionData();
            }
        };
    };

    var iPhoneInterface = function() {
        var commandQueue = new Array();

        $(document).bind('commandExecuted', function(e) {
            if (commandQueue.length && e.Data.Command == commandQueue[0]) {
                commandQueue.shift();
                executeCommand();
            }
        });

        var sendCommand = function(cmd) {
            var l = commandQueue.length;
            commandQueue.push(cmd);
            if (!l)
                executeCommand();
        };

        var executeCommand = function() {
            if (commandQueue.length > 0) {
                window.location = commandQueue[0];
            }
        };

        return {
            getPicture: function(titleText, cameraText, galleryText, cancelText) {
                var url = "javascriptcall:showPictureSourceDialog(" + titleText + "," + cameraText + "," + galleryText + "," + cancelText + ")";
                sendCommand(url);
            },
            getDeviceId: function() {
                var url = "javascriptcall:getDeviceId()";
                sendCommand(url);
            },
            setPageTitle: function(title) {
                if (menuVisible) {
                    var url = "javascriptcall:setPageTitle(" + (title || "") + ")";
                    sendCommand(url);
                }
            },
            showMenu: function() {
                if (!menuVisible) {
                    menuVisible = true;
                    var url = "javascriptcall:menuVisible(true)";
                    sendCommand(url);
                }
            },
            hideMenu: function() {
                var url = "javascriptcall:menuVisible(false)";
                sendCommand(url);
            },
            exitApplication: function() {
                //TODO
            },
            setUserSettings: function(data) {
                //TODO
            },
            getUserSettings: function() {
                //TODO
            },
            getAuthData: function() {
                //TODO
            },
            setAuthData: function(data) {
                //TODO
            },
            deleteAuthData: function() {
                //TODO
            },
            setModuleAction: function(action) {
                var url = "javascriptcall:setModuleAction(" + action + ")";
                sendCommand(url);
            },
            setNotifications: function(nr) {
                var url = "javascriptcall:setNotifications(" + nr + ")";
                sendCommand(url);
            },
            setBackbtnVisible: function(visible) {
                var url = "javascriptcall:setBackbtnVisible(" + visible.toString() + ")";
                sendCommand(url);
            },
            updateApplication: function(appStoreUrl) {
                var url = "javascriptcall:updateApplication(" + escape(appStoreUrl) + ")";
                sendCommand(url);
            },
            disconnect: function() {
                var url = "javascriptcall:disconnect()";
                sendCommand(url);
            },
            setServer: function(serverCode) {
                var url = "javascriptcall:setServer(" + serverCode + ")";
                sendCommand(url);
            },
            loadApplication: function(serverCode, username, password) {
                var url = "javascriptcall:loadApplication(" + serverCode + ", " + username + ", " + password + ")";
                sendCommand(url);
            },
            getConnectionData: function() {
                var url = "javascriptcall:getConnectionData()";
                sendCommand(url);
            }
        };
    };

    var phone = null;
    if (isAndroid)
        phone = new AndroidInterface();
    else if (isiPhone)
        phone = new iPhoneInterface();

    return {
        getPicture: function(titleText, cameraText, galleryText, cancelText) {
            if (phone && phone.getPicture)
                phone.getPicture(titleText, cameraText, galleryText, cancelText);
        },
        getDeviceId: function() {
            if (phone && phone.getDeviceId)
                return phone.getDeviceId();
        },
        hideSplashscreen: function() {
            if (phone && phone.hideSplashscreen)
                phone.hideSplashscreen();
        },
        showSplashscreen: function() {
            if (phone && phone.showSplashscreen)
                phone.showSplashscreen();
        },
        setPageTitle: function(title) {
            if (phone && phone.setPageTitle)
                phone.setPageTitle(title);
        },
        showMenu: function() {
            if (phone && phone.showMenu)
                phone.showMenu();
        },
        hideMenu: function() {
            if (phone && phone.hideMenu)
                phone.hideMenu();
        },
        setUserSettings: function(data) {
            if (phone && phone.setUserSettings)
                phone.setUserSettings(data);
        },
        getUserSettings: function() {
            if (phone && phone.getUserSettings)
                return phone.getUserSettings();
        },
        setAuthData: function(data) {
            if (phone && phone.setAuthData)
                phone.setAuthData(data);
        },
        getAuthData: function() {
            if (phone && phone.getAuthData)
                return phone.getAuthData();
        },
        deleteAuthData: function() {
            if (phone && phone.deleteAuthData)
                phone.deleteAuthData();
        },
        exitApplication: function() {
            if (phone && phone.exitApplication)
                phone.exitApplication();
        },
        setModuleAction: function(action) {
            if (phone && phone.setModuleAction)
                phone.setModuleAction(action);
        },
        setNotifications: function(nr) {
            if (phone && phone.setNotifications)
                phone.setNotifications(nr);
        },
        setBackbtnVisible: function(visible) {
            if (phone && phone.setBackbtnVisible)
                phone.setBackbtnVisible(visible);
        },
        disconnect: function(visible) {
            if (phone && phone.disconnect)
                phone.disconnect();
        },
        isAndroid: function() { return isAndroid; },
        isiOS: function() { return isiOS; },
        getMobilePlatformIndex: function() {
            if (isAndroid)
                return 1;
            else if (isiOS)
                return 2;

            return 0;
        },
        getMobilePlatformName: function() {
            if (isAndroid)
                return "android";
            else if (isiOS)
                return "ios";

            return "none";
        },
        updateApplication: function(appStoreUrl) {
            appStoreUrl = appStoreUrl || "";
            if (phone && phone.updateApplication)
                phone.updateApplication(appStoreUrl);
        },
        setServer: function(serverCode) {
            serverCode = serverCode || "";
            if (phone && phone.setServer)
                phone.setServer(serverCode);
        },
        loadApplication: function(serverCode, username, password) {
            serverCode = serverCode || "";
            if (phone && phone.loadApplication)
                phone.loadApplication(serverCode, username, password);
        },
        getConnectionData: function() {
            if (phone && phone.getConnectionData)
                phone.getConnectionData();
        }
    };
};